package org.acb.sgp.dao.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.acb.sgp.beans.ObjetoEvento;
import org.acb.sgp.dao.ObjetoEventoDao;
import org.acb.sgp.factory.ConnectionFactory;

public class ObjetoEventoDaoImpl implements ObjetoEventoDao {

    private Connection con;
    private CallableStatement cs;
    private ResultSet rs;

    private Connection getConnection() throws SQLException {
        return ConnectionFactory.getInstance().getConnection();
    }

    @Override
    public List<ObjetoEvento> listaObjetoEvento() {
        List<ObjetoEvento> objetos = new ArrayList<ObjetoEvento>();
        try {
            String sql = "{Call ListaObjetoEvento()}";
            con = getConnection();
            cs = con.prepareCall(sql);
            rs = cs.executeQuery();
            while (rs.next()) {
                objetos.add(llenar(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ObjetoEventoDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return objetos;
    }

    private ObjetoEvento llenar(ResultSet rs) throws SQLException {
        ObjetoEvento objeto = new ObjetoEvento();
        objeto.setCod_objeto(rs.getInt("cod_objeto"));
        objeto.setNom_objeto(rs.getString("nom_objeto"));
        return objeto;
    }

}
